package com.farm.shop.controller;

import com.farm.device.pojo.Device;
import com.farm.entity.PageResult;
import com.farm.entity.Result;
import com.farm.entity.StatusCode;
import com.farm.shop.pojo.CustDevice;
import com.farm.shop.service.CustDeviceService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.farm.constant.MessageConstant;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/custdevice")
public class CustDeviceController {
    @Autowired
    private CustDeviceService custDeviceService;

    /**
     * 查询设备
     * @param page
     * @param size
     * @param map
     * @return
     */
    @GetMapping("/dimfindPage")
    public Result findPage(@RequestParam(value = "page",defaultValue = "0",required = false) Integer page, @RequestParam(value = "size",defaultValue = "10",required = false) Integer size, @RequestParam Map<String,Object> map){
        PageResult pageResult= null;
        try {
            Page<CustDevice> pageList = custDeviceService.findPage(map, page, size);
            pageResult = new PageResult(pageList.getTotal(),pageList.getResult());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, StatusCode.OK,MessageConstant.SELECT_FARM_CUSTDEVICE_FAIL);
        }
        return new Result(true,StatusCode.OK,MessageConstant.SELECT_FARM_CUSTDEVICE_SUCCESS,pageResult);
    }

    /**
     * 删除设备
     * @param id
     * @return
     */
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Integer id){
        try {
            custDeviceService.deleteById(id);
            return new Result(true,StatusCode.OK,MessageConstant.DELETE_FARM_CUSTDEVICE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,MessageConstant.DELETE_FARM_CUSTDEVICE_FAIL);
        }
    }

    /**
     * 修改运行状态
     * @param id
     * @return
     */
    @PutMapping("/update/{id}")
    public Result update(@PathVariable Integer id){
        custDeviceService.update(id);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /**
     * 修改安装状态
     * @param id
     * @return
     */
    @PutMapping("/update2/{id}")
    public Result update2(@PathVariable Integer id){
        custDeviceService.update(id);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /**
     * 手动添加
     * @return
     */
    @PostMapping("/addShop")
    public Result addShop(@RequestParam("cid")Integer cid,@RequestParam("did") Integer did, @RequestParam("code")String code ,@RequestParam("position")String position) {
        try {
            custDeviceService.addShop(cid, did, code, position);
            return new Result(true, StatusCode.OK, "添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.OK, "添加失败");
        }
    }

    /**
     * 根据大田id查询相关的设备信息
     * @param farmlandId
     * @return
     */
    @RequestMapping("/findCustDevicesByfarmlandId")
    public List<CustDevice> findCustDevicesByfarmlandId(@RequestParam("farmlandId")Integer farmlandId){
        return custDeviceService.findCustDevicesByfarmlandId(farmlandId);
    }

    /**
     * 根据客户的设备id修改安装状态以及设备位置
     * @param cdid
     * @param position
     */
    @RequestMapping("/updateByCustDeviceId/{cdid}/{position}")
    public void updateByCustDeviceId(@PathVariable("cdid") Integer cdid, @PathVariable("position") String position){
        custDeviceService.updateByCustDeviceId(cdid,position);
    }

    @GetMapping("findById/{did}")
    public Result findById(@PathVariable("did") Integer did){
        List<Device> deviceList = null;
        try {
            deviceList = custDeviceService.findById(did);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,StatusCode.OK,MessageConstant.SELECT_FARM_DEVICE_FAIL);
        }
        return new Result(true,StatusCode.OK,MessageConstant.SELECT_FARM_DEVICE_SUCCESS,deviceList);
    }
}
